<!--
-->

<template>
    <vxe-modal v-model="visible" :position="{top: '60px'}" :title="title" destroy-on-close  :min-width="layout.modalMinWidth(400)" resize show-footer show-maximize :width="layout.modalWidth(440)" :height="layout.modalHeight()" @close="cancelClick">
        <el-form ref="ELForm" v-loading="isLoading" :model="state.model" :rules="rules" :validate-on-rule-change="false" class="ELForm" inline label-width="80px">
            <el-form-item :style="layout.col1" label="账号" prop="userName">
                <el-input v-model="state.model.userName" disabled/>
            </el-form-item>
            <el-form-item :style="layout.col1" label="密码" prop="newPassword">
                <g-password-input v-model="state.model.newPassword"/>
            </el-form-item>
            <el-form-item :style="layout.col1" label="确认密码" prop="repassword">
                <g-password-input v-model="state.model.repassword"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>

</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js"
import {checkPassword} from "@/utils/utils.js"

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message} = window;

const Model = () => {
    return {
        id: 0,
        password: undefined,
        realName: undefined,
        headerImg: undefined,
        gender: 1,
        identityCard: undefined,
        phone: undefined,
        email: undefined,
        weChat: undefined,
        qYWeChat: undefined,
        userType: 0,
        sysOrgId: 0,
        sysPostId: 0,
        isAdmin: 0,
        userStatus: 0,
        userName: undefined,
        newPassword: undefined,
        repassword: undefined,
        roleIds: [],
    }
}

// 校验规则
const rules = computed(() => {
    return {
        newPassword: [{required: true, validator: customRules.checkPassword}],
        repassword: [{required: true, validator: customRules.checkRepassword}],
    }
})

// 自定义校验规则
const customRules = {
    // 自定义校验 密码
    checkPassword: (rule, value, callback) => {
        // if (!checkPassword(value)) return callback(new Error("密码不少于8位包含大小写、不连续数字、特殊字符"))
        if (!value||value.length<6) return callback(new Error("密码不少于6位字符"))
        return true;
    },
    // 自定义校验 确认密码
    checkRepassword: (rule, value, callback) => {
        if (!value || value.length < 6 || value !== state.model.newPassword) {
            return callback(new Error("请确认密码是否一致"));
        }
        return true;
    }
};

// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    state.model = {...state.model, ...state.options}
    // 初始化数据
}

// 校验表单
function beforeSubmit() {
    ELForm.value.validate(isOk => {
        isOk && submit()
    })
}

// 提交表单
async function submit() {
    isSubmitting.value = true
    console.log("submit: ", state.model)
    const body = {Id: state.model.id, NewPassword: state.model.newPassword}
    console.log("重置密码: ", JSON.stringify(body))
    const {success, data} = await api.ResetUserPassword(body);
    isSubmitting.value = false
    if (!success) return 0;
    $message.success("操作成功")
    submitSuccess()
}

onMounted(() => {
})
defineExpose({
    show
})
</script>

<style lang="scss" scoped>
.EditForm {

}
</style>
